﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TB.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.WebControls;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common;
using Lenovo.CFI.BLL.Sys;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicScheduleList : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringID();
                Topic topic = null;
                if (id.HasValue)
                {
                    topic = topicBl.GetTopicByID(id.Value);
                }
                if (topic == null) ErrorHelper.RaiseNoValueError();

                ViewState["topicid"] = topic.ID;


                if (topicBl.IsReviewer(topic.ID, UserHelper.UserName))
                {
                    this.DivAgent.Visible = true;

                    this.OuacAgent.Text = topicBl.GetAgent(topic.ID, UserHelper.UserName);
                }
                else
                {
                    this.DivAgent.Visible = false;
                }

                this.BindData();
            }
        }


        Dictionary<int, Idea> ideasDic = null;
        List<ScheduleDetail> scheduleDetails = null;
        private void BindData()
        {
            int topicID = Convert.ToInt32(ViewState["topicid"]);
            ScheduleBl scheduleBl = new ScheduleBl();
            this.scheduleDetails = scheduleBl.GetScheduleWithTimeTable(topicID);

            ideasDic = new Dictionary<int, Idea>();
            foreach (Idea idea in new IdeaBl().GetIdeaByScheduleTopic(topicID))
                ideasDic.Add(idea.ID, idea);

            this.scheduleDetails.RemoveAt(0);        // 去掉开始

            this.GvList.DataSource = this.scheduleDetails;
            this.GvList.DataBind();
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 设置行号
                ((Literal)(e.Row.FindControl("LtrNo"))).Text = String.Format("{0}", e.Row.DataItemIndex + 1);

                ScheduleDetail scheduleDetail = (ScheduleDetail)e.Row.DataItem;


                ((Literal)e.Row.FindControl("LtrStartTime")).Text = scheduleDetail.StartTime.ToString("HH:mm");

                if (scheduleDetail.EventType == ScheduleEventType.Idea)
                {
                    Idea idea = ideasDic[scheduleDetail.IdeaID.Value];
                    ((Literal)e.Row.FindControl("LtrTitle")).Text = idea.Title;
                    ((Literal)e.Row.FindControl("LtrOwner")).Text = idea.Owner;
                }
                else if (scheduleDetail.EventType == ScheduleEventType.Break)
                {
                    ((Literal)e.Row.FindControl("LtrTitle")).Text = "Break";
                }
            }
        }

        protected void BtnExport_Click(object sender, EventArgs e)
        {
            FileDUHelper.Download("Schedule.xls", new ScheduleBl().GenerateTimeTable4Excel((int)ViewState["topicid"]));
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                topicBl.SetAgent(Convert.ToInt32(ViewState["topicid"]), UserHelper.UserName, this.OuacAgent.Text);

                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.Show(aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.Handle(ex);
            }
        }
    }
}